package com.rem.designPattern.designPrinciple.interfaceSegregation.improve;

/**
 * 设计模式七大原则--接口隔离
 * 客户端不应该依赖它不需要的接口，即一个类对另一个类的依赖应该建立在最小的接口上
 * <p>
 * 需求：Xservice依赖A需要使用 1、2、3接口,但不要4、5，Yservice依赖B需要1、4、5接口，但不要2、3
 * <p>
 * 改进 将接口进行拆分最小粒度 只提供需要的方法
 *
 * @author Rem
 * @date 2022-11-11
 */
public class Segregation2 {
    public static void main(String[] args) {
        Xservice xservice = new Xservice();
        A a = new A();
        xservice.depend1(a);
        xservice.depend2(a);
        xservice.depend3(a);

        Yservice yservice = new Yservice();
        B b = new B();
        yservice.depend1(b);
        yservice.depend4(b);
        yservice.depend5(b);
    }
}

class Xservice {
    public void depend1(Interface2 i) {
        i.operation1();
    }

    public void depend2(Interface2 i) {
        i.operation2();
    }


    public void depend3(Interface2 i) {
        i.operation3();
    }

}

class Yservice {
    public void depend1(Interface3 i) {
        i.operation1();
    }

    public void depend4(Interface3 i) {
        i.operation4();
    }

    public void depend5(Interface3 i) {
        i.operation5();
    }
}

interface Interface1 {

    void operation1();
}

interface Interface2 extends Interface1 {

    void operation2();

    void operation3();
}

interface Interface3 extends Interface1 {

    void operation4();

    void operation5();
}

class A implements Interface2 {

    @Override
    public void operation1() {
        System.out.println("A 实现了 operation1");
    }

    @Override
    public void operation2() {
        System.out.println("A 实现了 operation2");
    }

    @Override
    public void operation3() {
        System.out.println("A 实现了 operation3");
    }

}

class B implements Interface3 {

    @Override
    public void operation1() {
        System.out.println("B 实现了 operation1");
    }

    @Override
    public void operation4() {
        System.out.println("B 实现了 operation4");
    }

    @Override
    public void operation5() {
        System.out.println("B 实现了 operation5");
    }
}